﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 学生信息管理系统
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void StudentLogin_Click(object sender, EventArgs e)
        {
                    string cn_str = pub.CN_STR;
                    OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
                    cn.Open();
                    string select = "select * from 学生登录 where 用户名='" + txtName.Text + "'";
                    OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
                    OleDbDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())                                         //读取数据
                    {
                        string str = txtPassword.Text.ToString();
                        string str2 = reader.GetValue(1).ToString();
                        if (str == str2)                                         //判断密码是否正确
                        {
                            this.Hide();
                            MessageBox.Show("登录成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            Student f2 = new Student();
                            f2 = new Student(txtName.Text);
                            DialogResult r = f2.ShowDialog();
                            f2.Closed += (ss, ee) => { this.Close(); };
                        }
                        else
                        {
                            MessageBox.Show("密码错误！", "密码检验！", MessageBoxButtons.OK);
                            txtPassword.Text = "";//密码错误
                            txtName.Focus();//设置焦点
                        }
                        
                    }


                    else
                    {
                        MessageBox.Show("不存在此用户！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtName.Text = "";
                        txtPassword.Text = "";
                        txtName.Focus();                                      //用户错误
                    }

                    //关闭数据连接
                    cn.Close();

        }

        private void TeacherLogin_Click(object sender, EventArgs e)
        {
            string cn_str = pub.CN_STR;
                    OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
                    cn.Open();
                    string select = "select * from 教师登录 where 用户名='" + txtName.Text + "'";
                    OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
                    OleDbDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())                                         //读取数据
                    {
                        string str = txtPassword.Text.ToString();
                        string str2 = reader.GetValue(1).ToString();
                        if (str == str2)                                         //判断密码是否正确
                        {
                            this.Hide();
                            MessageBox.Show("登录成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            Teacher f2 = new Teacher();
                            f2 = new Teacher(txtName.Text);
                            DialogResult r = f2.ShowDialog();
                            f2.Closed += (ss, ee) => { this.Close(); };
                        }
                        else
                        {
                            MessageBox.Show("密码错误！", "密码检验！", MessageBoxButtons.OK);
                            txtPassword.Text = "";//密码错误
                            txtName.Focus();//设置焦点
                        }
                        
                    }


                    else
                    {
                        MessageBox.Show("不存在此用户！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtName.Text = "";
                        txtPassword.Text = "";
                        txtName.Focus();                                      //用户错误
                    }

                    //关闭数据连接
                    cn.Close();

            }
            private void ManagerLogin_Click(object sender, EventArgs e)
            {
            string cn_str = pub.CN_STR;
            OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
            cn.Open();
            string select = "select * from 管理员登录 where 用户名='" + txtName.Text + "'";
            OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
            OleDbDataReader reader = cmd.ExecuteReader();
            if (reader.Read())                                         //读取数据
            {
                string str = txtPassword.Text.ToString();
                string str2 = reader.GetValue(1).ToString();
                if (str == str2)                                         //判断密码是否正确
                {
                    this.Hide();
                    Manager ff = new Manager();
                    ff.Text += txtName.Text;
                    MessageBox.Show("登录成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    ff.Show();
                }
                else
                {
                    MessageBox.Show("密码错误！", "密码检验！", MessageBoxButtons.OK);
                    txtPassword.Text = "";//密码错误
                    txtName.Focus();//设置焦点
                }

            }


            else
            {
                MessageBox.Show("不存在此用户！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtName.Text = "";
                txtPassword.Text = "";
                txtName.Focus();                                      //用户错误
            }

            //关闭数据连接
            cn.Close();
        }

            private void ManagerLogin_Click_1(object sender, EventArgs e)
            {
                string cn_str = pub.CN_STR;
                OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
                cn.Open();
                string select = "select * from 管理员登录 where 用户名='" + txtName.Text + "'";
                OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
                OleDbDataReader reader = cmd.ExecuteReader();
                if (reader.Read())                                         //读取数据
                {
                    string str = txtPassword.Text.ToString();
                    string str2 = reader.GetValue(1).ToString();
                    if (str == str2)                                         //判断密码是否正确
                    {
                        this.Hide();
                        MessageBox.Show("登录成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        Manager f2 = new Manager();
                        f2.Show();
                        f2.Closed += (ss, ee) => { this.Close(); };
                    }
                    else
                    {
                        MessageBox.Show("密码错误！", "密码检验！", MessageBoxButtons.OK);
                        txtPassword.Text = "";//密码错误
                        txtName.Focus();//设置焦点
                    }

                }


                else
                {
                    MessageBox.Show("不存在此用户！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtName.Text = "";
                    txtPassword.Text = "";
                    txtName.Focus();                                      //用户错误
                }

                //关闭数据连接
                cn.Close();
            }

            private void Login_FormClosing(object sender, FormClosingEventArgs e)
            {
                //if (DialogResult.OK == MessageBox.Show("你确定要关闭应用程序吗？", "关闭提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                //{
                //    this.FormClosing -= new FormClosingEventHandler(this.Login_FormClosing);//为保证Application.Exit();时不再弹出提示，所以将FormClosing事件取消
                //    Application.Exit();//退出整个应用程序
                //}
                //else
                //{
                //    e.Cancel = true;  //取消关闭事件
                //}
            }

       

    }
}
